package com.example.recordingpen_datacollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.Identification;
import com.example.recordingpen_datacollection.entity.Query.UserDevQue;
import com.example.recordingpen_datacollection.entity.Query.UserListQue;
import com.example.recordingpen_datacollection.entity.UserMessage;
import com.example.recordingpen_datacollection.entity.Userdevmess;
import com.example.recordingpen_datacollection.entity.Vo.UserLimitQuantityVo;
import com.example.recordingpen_datacollection.entity.Vo.UserMaxTimeVo;
import com.example.recordingpen_datacollection.entity.Vo.UserMessVo;
import com.example.recordingpen_datacollection.entity.Vo.UserTimeLimitVo;
import com.example.recordingpen_datacollection.mapper.UserMessageMapper;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
import com.example.recordingpen_datacollection.service.IdentificationService;
import com.example.recordingpen_datacollection.service.LimitRatioService;
import com.example.recordingpen_datacollection.service.UserMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.recordingpen_datacollection.service.UserdevmessService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2024-06-03
 */
@Service
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, UserMessage> implements UserMessageService {
    @Autowired
    private IdentificationService identificationService;
    @Autowired
    private UserdevmessService userdevmessService;
    @Autowired
    private LimitRatioService limitRatioService;
    @Autowired
    private UserMessageService userMessageService;

    @Transactional
    @Override
    public boolean setUserMessage(UserMessVo userMessVo,Integer adminId) {
        UserMessage userMessage = new UserMessage();
        userMessage.setUname(userMessVo.getUname());
        userMessage.setUaccount(userMessVo.getUaccount());
        userMessage.setUpassword(userMessVo.getUpassword());
        userMessage.setAdminId(adminId);
        if(StringUtils.isNotEmpty(userMessVo.getMaxTime())){
            userMessage.setMaxTime(TimeJudgeUtils.strData(userMessVo.getMaxTime()));
        } else {
            String monthTime = TimeJudgeUtils.addNowMonthTime("", 18);
            userMessage.setMaxTime(TimeJudgeUtils.strData(monthTime));
        }

        if(userMessVo.getLimitQuantity() == null || userMessVo.getLimitQuantity()<=0){
            userMessage.setLimitQuantity(18);
            userMessage.setTotalAmount(18);
        } else {
            userMessage.setLimitQuantity(userMessVo.getLimitQuantity());
            userMessage.setTotalAmount(userMessVo.getLimitQuantity());
        }


        boolean save = this.save(userMessage);

        if(save){
            Integer ratio = 18;
            if(userMessVo.getRatio() !=null && userMessVo.getRatio() >0){
                ratio = userMessVo.getRatio();
            }
            boolean saveRatio = limitRatioService.setLimitRatio(ratio, userMessage.getUid());
            if(!saveRatio){
                throw new JWException(500,"标识添加异常");
            }

            Identification identification = new Identification();
            identification.setUid(userMessage.getUid());
            identification.setCompanyLogo(userMessVo.getLogo());
            Integer defaultValue = userMessVo.getDefaultValue();
            if (defaultValue !=null && defaultValue>18){
                defaultValue = null;
            }
            identification.setDefaultValue(defaultValue);
            identification.setVoiceDefault(userMessVo.getVoiceDefault());
            boolean save1 = identificationService.save(identification);
            if(!save1){
                throw new JWException(500,"标识添加异常");
            }
        }
        return save;
    }

    @Override
    public boolean alterUserPass(Integer value, String newPass) {

        UserMessage userMessage = new UserMessage();
        userMessage.setUpassword(newPass);
        userMessage.setUid(value);
        boolean update = this.updateById(userMessage);
        return update;
    }

    @Override
    public RLIST getUserDev(UserDevQue userDevQue, Integer Uid) {

        QueryWrapper wrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(userDevQue.getUsername())){
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("uaccount",userDevQue.getUsername());
            UserMessage one = userMessageService.getOne(wrapper1);
            wrapper.eq("uid",one.getUid());
        }
        if(StringUtils.isNotEmpty(userDevQue.getDevname())){
            wrapper.eq("account",userDevQue.getDevname());
        }
        if(StringUtils.isNotEmpty(userDevQue.getExclude())){
            wrapper.notLike("account",userDevQue.getExclude());
        }
        if(Uid != null && Uid > 0){
            wrapper.eq("uid",Uid);
        }

        wrapper.orderByDesc("udid");
        Page<Userdevmess> page = new Page<>(userDevQue.getPageNo(), userDevQue.getPageSize());
        userdevmessService.page(page, wrapper);
        long total = page.getTotal();
        List<Userdevmess> list = page.getRecords();

        long numPage = 0;
        if(total == 0){
            numPage = 1L;
        } else if(list.size() == 0){
            numPage = 1L;
        } else {
            // 每页条数
            numPage = (long) Math.ceil(total / (double)userDevQue.getPageSize()) ;
        }
        if(list.size() == 0){
            return RLIST.error().message("暂无数据") ;
        }
        return RLIST.ok().data("data",list).data("total",total).data("numPage",numPage);
    }

    @Override
    public boolean verifyLimit(Integer value) {
        UserMessage byId = this.getById(value);

        if(byId != null && byId.getLimitQuantity() > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean setUserMaxTime(UserMaxTimeVo userMaxTimeVo) {
        if(userMaxTimeVo.getTime() ==null){
            return false;
        }
        UserMessage userMessage = new UserMessage();
        if(StringUtils.isNotEmpty(userMaxTimeVo.getTime())){
            userMessage.setMaxTime(TimeJudgeUtils.strData(userMaxTimeVo.getTime()));
        }


        userMessage.setUid(userMaxTimeVo.getUserId());

        boolean update = this.updateById(userMessage);
        return update;
    }

    @Override
    public boolean setUserLimitQuantity(UserLimitQuantityVo userLimitQuantityVo, UserMessage byId) {
        if(userLimitQuantityVo.getLimitQuantity() == null){
            return false;
        }
        UserMessage userMessage = new UserMessage();

        if(userLimitQuantityVo.getLimitQuantity() != null && userLimitQuantityVo.getLimitQuantity() >0){
            userMessage.setLimitQuantity(userLimitQuantityVo.getLimitQuantity());
        }
        userMessage.setUid(userLimitQuantityVo.getUserId());
        userMessage.setTotalAmount(byId.getTotalAmount()+userLimitQuantityVo.getLimitQuantity());
        boolean update = this.updateById(userMessage);
        return update;
    }

    @Override
    public RLIST getUserListPage(UserListQue userListQue) {
        QueryWrapper wrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(userListQue.getUname())){
            wrapper.eq("uname",userListQue.getUname());
        }


        wrapper.orderByDesc("uid");
        Page<UserMessage> page = new Page<>(userListQue.getPageNo(), userListQue.getPageSize());
        this.page(page, wrapper);
        long total = page.getTotal();
        List<UserMessage> list = page.getRecords();

        long numPage = 0;
        if(total == 0){
            numPage = 1L;
        } else if(list.size() == 0){
            numPage = 1L;
        } else {
            // 每页条数
            numPage = (long) Math.ceil(total / (double)userListQue.getPageSize()) ;
        }
        if(list.size() == 0){
            return RLIST.error().message("暂无数据") ;
        }
        return RLIST.ok().data("data",list).data("total",total).data("numPage",numPage);

    }

    @Override
    public boolean setUserTimeLimit(UserTimeLimitVo userTimeLimitVo, UserMessage byId) {
        UserMessage userMessage = new UserMessage();
        userMessage.setUid(userTimeLimitVo.getUserId());
        userMessage.setTotalAmount(byId.getTotalAmount()+userTimeLimitVo.getLimitQuantity());
        if(userTimeLimitVo.getLimitQuantity() != null && userTimeLimitVo.getLimitQuantity() >0){
            userMessage.setLimitQuantity(userTimeLimitVo.getLimitQuantity());
        }

        if(StringUtils.isNotEmpty(userTimeLimitVo.getTime())){
            userMessage.setMaxTime(TimeJudgeUtils.strData(userTimeLimitVo.getTime()));
        }
        boolean update = this.updateById(userMessage);
        return update;
    }
}